import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_pagewise/flutter_pagewise.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:loxen/mode/study/study_info_bean.dart';
import 'package:loxen/routes/routes.dart';
import 'package:loxen/view/study/study_list.dart';
import 'package:loxen/view/study/study_tabbar.dart';
import 'package:loxen/viewmode/search_result_viewmodel.dart';
import 'package:loxen/viewmode/study_index_viewmodel.dart';
import 'package:loxen/widget/empty_widget.dart';
import 'package:loxen/widget/fail_widget.dart';
import 'package:loxen/widget/loading_widget.dart';
import 'package:loxen/widget/my_subassembly.dart';
import 'package:provider/provider.dart';
import 'package:loxen/viewmode/base_view_model.dart';

class StudyIndexWidget extends StatefulWidget {
  const StudyIndexWidget({Key key}) : super(key: key);

  @override
  _StudyIndexWidgetState createState() => _StudyIndexWidgetState();
}

class _StudyIndexWidgetState extends State<StudyIndexWidget> with AutomaticKeepAliveClientMixin {
  PagewiseLoadController _pageController;

  @override
  void initState() {
    super.initState();
    _pageController = PagewiseLoadController<StudyInfo>(
        pageFuture: (page) {
          return context.read<StudyViewModel>().nextData(page);
        },
        pageSize: 5);
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
        appBar: PreferredSize(
          preferredSize: Size(double.infinity, 95.h),
          child: Container(
            color: Theme.of(context).primaryColor,
            child: SafeArea(
              child: Stack(
                children: <Widget>[
                  Positioned(
                    left: 0,
                    child: MySubassembly.getSearchIcon(() {
                      var data =
                          Provider.of<StudyViewModel>(context, listen: false).currentCategory;
                      Navigator.of(context).pushNamed(Routes.search, arguments: {
                        'mType':
                            Provider.of<StudyViewModel>(context, listen: false).currentCategory == 0
                                ? SearchType.KNOWLEDGE
                                : SearchType.CASE,
                        'studyCategory': data,
                        'hintText': data == 0 ? "输入知识标题" : "输入应用标题"
                      });
                    }),
                  ),
                  Center(
                    child: StudyTabBar(),
                  )
                ],
              ),
            ),
          ),
        ),
        body: Container(
            color: Color(0xFFF2F2F2),
            child: Column(
              children: <Widget>[
                Selector<StudyViewModel, BaseState>(
                  builder: (_, state, __) {
                    if (state == BaseState.CONTENT) {
                      return Padding(
                        padding: EdgeInsets.only(left: 36.w, top: 10.h, bottom: 10.h),
                        child: StudyTabBar2(),
                      );
                    }
                    return Text('');
                  },
                  selector: (_, model) => model.mState,
                ),
                Expanded(
                    child: Selector<StudyViewModel, BaseState>(
                  builder: (_, state, __) {
                    if (state == BaseState.LOADING) {
                      return LoadingWidget();
                    }
                    if (state == BaseState.CONTENT) {
                      return Container(
                        margin: EdgeInsets.only(left: 36.w, right: 36.w),
                        child: StudyList(),
                      );
                    }
                    if (state == BaseState.EMPTY) {
                      return EmptyWidget();
                    }
                    if (state == BaseState.FAIL) {
                      return FailWidget(
                        onClick: () {
                          context.read<StudyViewModel>().loadData();
                        },
                      );
                    }
                    return Text('');
                  },
                  selector: (_, model) => model.mState,
                ))
              ],
            )));
  }

  @override
  bool get wantKeepAlive => true;
}
